<?php
namespace app\back\controller;
use Qiniu\Auth;
use think\Controller;
use think\Request;
use think\Db;
use Qiniu\Storage\BucketManager;
use Qiniu\Storage\UploadManager; 
use think\Session;
/*平台活动管理*/
class Activity extends Common
{   
	//活动列表
    public function index()
    {   
    	  //获取banner数据
    	 $activity = Db::name('activity')->where(['checkId'=>'1'])->order("id desc")->paginate(5,false);
         $page = $activity->render();
         $this->assign('activity',$activity);
         $this->assign('page',$page);
       return $this->fetch('Activity/index'); 
    }
    
     //单个删除活动
     public function del_activity()
    {
        $aId = input('adid');
      $data = Db::name('activity')->where('id',$aId)->find();
        $activity_img = substr($data['goodsThums'],33);
        if(!empty($activity_img)){
      require_once VENDOR_PATH.'qiniu/php-sdk/autoload.php';
        // 构建鉴权对象
        $auth = new Auth();
         // 要上传的空间
        $bucket ='banner';
         $domain = config('DOMAIN');
        // 初始化 BucketManager 对象并进行文件的删除
        $deleteBuc = new BucketManager($auth);
        // 调用 BucketManager 的 delete 方法进行文件的删除
        list($ret, $err)= $deleteBuc->delete($bucket, $activity_img);
        if($err == null){
            $list = Db::name('activity')->where("id",$aId)->delete();
            if($list){
                 return json(['state' => '1', 'msg' => '删除成功!']);
            }else{
                return json(['state' => '0', 'msg' => '删除失败再试试!']);
            }
        }
      }else{
        $list = Db::name('activity')->where("id",$aId)->delete();
            if($list){
                 return json(['state' => '1', 'msg' => '删除成功!']);
            }else{
                return json(['state' => '0', 'msg' => '删除失败再试试!']);
            }
      }
    
    }
    //活动审核列表
    public function activity_check()
    {   
      //获取banner数据
       $activity = Db::name('activity')->where(['checkId'=>'-1'])->order("id desc")->paginate(5,false);
         $page = $activity->render();
         $this->assign('activity',$activity);
         $this->assign('page',$page);
       return $this->fetch('Activity/activity_check'); 
    }
    //活动审核的操作
    public function activity_start()
    {   
        $aId = input('aId');
        $checkId = input('checkId');
        $result =Db::name('activity')->where(['id'=>$aId])->setField('checkId',$checkId); 
        if($result){
          $rd=Db::name('activity')->where(['id'=>$aId])->field('captain_id,tid,name')->find();
        //  print_r($rs);die;
          $userInfo=Db::name('user_info')->where(['uid'=>$rd['captain_id']])->field('nickname,accid,userPhoto')->find();
          if(!empty($userInfo['accid'])){
            // //获取accid
            $accid=$userInfo['accid'];
            //活动建群
             $headers=$this->setHeader();
             $members=json_encode(["$accid"]);//邀请的人
              $post_data=['tname'=>$rd['name'],'owner'=>$userInfo['accid'],'members'=>$members,'msg'=>'加群啦','magree'=>'0','joinmode'=>'0'];
              $ch = curl_init();
              curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
              curl_setopt($ch, CURLOPT_URL,'http://api.netease.im/nimserver/team/create.action');
              curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
              curl_setopt($ch, CURLOPT_POST, 1);
              curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
              $output = curl_exec($ch); 
              $rs=json_decode($output,true);
              if($rs['code']=='200'){
                $data=['tid'=>$rs['tid']];
                $sdata=['tid'=>$rs['tid'],'captain_id'=>$rd['captain_id']];
                Db::name('activity')->where(['id'=>$aId])->update($data);
                Db::name('captain_flock')->insert($sdata);
              }
           //群发关注领队发布的活动
           //判断领队是否群发过
           $data1=['uid'=>$rd['captain_id'],'genreId'=>'3','activity_id'=>$aId,'nickname'=>$userInfo['nickname'],'userPhoto'=>$userInfo['userPhoto'],'create_time'=>time()];
             Db::name('user_message')->insert($data1);
          
          }
         return json(['state' => '1', 'msg' => '操作成功!']);
        }else{
         return json(['state' => '0', 'msg' => '操作失败再试试!']);
        }
    } 
 }   